package cn.itrip.biz.service.impl;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.pojo.ItripUserLinkUser;
import cn.itrip.biz.service.ItripUserLinkUserService;
import cn.itrip.common.DtoUtils;
import cn.itrip.common.RedisUtil;
import cn.itrip.dao.mapper.ItripUserLinkUserMapper;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ItripUserLinkUserMapperServiceImpl implements ItripUserLinkUserService {
    Logger logger  =  Logger.getLogger(ItripUserLinkUserMapperServiceImpl.class);
    @Resource
    private ItripUserLinkUserMapper itripUserLinkUserMapper;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private HttpServletRequest request;

    public Dto selectAll(String linkUserName,Long userId) {
        //获取header中的token
        String token=request.getHeader("token");
        //根据获取的key获取redis中对应的value
        String key=redisUtil.getValue(token);
        //把JSON转换成ItripUser对象
        ItripUser user= JSONObject.parseObject(key,ItripUser.class);
        //查询所有旅客信息
        //调用数据层方法
        List<ItripUserLinkUser>list= null;
        try {
            list = itripUserLinkUserMapper.selectAll(linkUserName,user.getId());
        }catch (Exception e) {
            //打印异常信息
            logger.debug("出错了"+e);
            return DtoUtils.getExcetpionDto(e,"30002");
        }
        return DtoUtils.getSuccesDto(list);
    }

    @Override
    public Dto<ItripUserLinkUser> insert(ItripUserLinkUser itripUserLinkUser) {
        //获取header中的token
        String token=request.getHeader("token");
        //根据获取的key获取redis中对应的value
        String key=redisUtil.getValue(token);
        //把JSON转换成ItripUser对象
        ItripUser user= JSONObject.parseObject(key,ItripUser.class);
        //设置当前用户id
        itripUserLinkUser.setUserId(user.getId());
        //增加
        int num= 0;
        try {
            num = itripUserLinkUserMapper.insert(itripUserLinkUser);
        }catch (Exception e) {
            //打印异常信息
            logger.debug("出错了"+e);
            return DtoUtils.getExcetpionDto(e,"30002");
        }
        //判断是否增加成功
        if(num>0){
            return DtoUtils.getSuccesDto("50000");
        }
        return DtoUtils.getErrorDto("错误","50001");
    }

    @Override
    public Dto updateByPrimaryKey(ItripUserLinkUser itripUserLinkUser) {
        //修改旅客信息
        int num= 0;
        try {
            num = itripUserLinkUserMapper.updateByPrimaryKey(itripUserLinkUser);
        } catch (Exception e) {
            //打印异常信息
            logger.debug("出错了"+e);
            return DtoUtils.getExcetpionDto(e,"30002");
        }
        //判断是否修改成功
        if(num>0){
            return DtoUtils.getSuccesDto("50000");
        }
        return DtoUtils.getErrorDto("错误","50001");
    }
    @Override
    public Dto deleteById(int[] id_arr) {
        //删除
        int num= 0;
        try {
            num = itripUserLinkUserMapper.deleteByKey(id_arr);
        }catch (Exception e) {
            //打印异常信息
            logger.debug("出错了"+e);
            return DtoUtils.getExcetpionDto(e,"30002");
        }
        if(num>0){
            return DtoUtils.getSuccesDto("50000");
        }
        return DtoUtils.getErrorDto("错误","50001");
    }
}
